package com.founder.creater.utils;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStreamWriter;

/**
 * 版权所有：北京北大方正电子有限公司</br>
 * 
 * ThreadManager </br>
 * 
 * @author zhou.wenkai ,Created on 2015-9-2 14:16:00</br> 
 * 		   Major Function：一个简易的线程池管理类，提供三个线程池 </br>
 * 
 *         注:如果您修改了本类请填写以下内容作为记录，如非本人操作劳烦通知，谢谢！！！</br>
 * @author mender，Modified Date Modify Content:
 */
public class TextFileHelper {
	
	// 单例模式
	private static TextFileHelper instance = null;
	private TextFileHelper() {}
	public static TextFileHelper getInstance() {
		if(instance == null) {
			instance = new TextFileHelper();
		}
		return instance;
	}
	
	/**
	 * 在第一行插入文本
	 * 
	 * @param file
	 * @param insertStr
	 * @return void:
	 * @version 1.0 
	 * @date 2015-9-3
	 * @Author zhou.wenkai
	 */
    public void insertHeaderText(File file, String insertStr) {
    	BufferedReader	bufr = null;
    	BufferedWriter 	bufw = null;
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
			bufr = new BufferedReader(isr);
//			bufr = new BufferedReader(new FileReader(file));
			StringBuffer buf = new StringBuffer();
			String line = null;
			
			// 保存插入文本
			buf.append(insertStr);
			buf = buf.append(System.getProperty("line.separator"));
			while ((line = bufr.readLine()) != null) {
				// 保存原文本
				buf = buf.append(line);
				buf = buf.append(System.getProperty("line.separator"));
			}
			OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
			bufw = new BufferedWriter(osw);
			bufw.write(buf.toString());
            bufw.close();
	        
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(bufr != null) {
					bufr.close();
				}
				if(bufw != null) {
					bufw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    }
    
	/**
	 * 在最后一行插入文本
	 * 
	 * @param file
	 * @param insertStr
	 * @return void:
	 * @version 1.0 
	 * @date 2015-9-3
	 * @Author zhou.wenkai
	 */
    public void insertFooterText(File file, String insertStr) {
    	BufferedReader	bufr = null;
    	BufferedWriter 	bufw = null;
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
			bufr = new BufferedReader(isr);
//			bufr = new BufferedReader(new FileReader(file));
			StringBuffer buf = new StringBuffer();
			String line = null;
			
			while ((line = bufr.readLine()) != null) {
				// 保存原文本
				buf = buf.append(line);
				buf = buf.append(System.getProperty("line.separator"));
			}
			// 保存插入文本
			buf = buf.append(System.getProperty("line.separator"));
			buf.append(insertStr);
			OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
			bufw = new BufferedWriter(osw);
			bufw.write(buf.toString());
            bufw.close();
	        
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(bufr != null) {
					bufr.close();
				}
				if(bufw != null) {
					bufw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    }
    
	/**
	 * 检查第一行是否包含制定文本
	 * 
	 * @param file
	 * @param containsStr
	 * @return
	 * @return boolean:
	 * @version 1.0 
	 * @date 2015-9-3
	 * @Author zhou.wenkai
	 */
    public boolean isHeaderContainsText(File file, String containsStr) {
    	BufferedReader	bufr = null;
    	boolean 		flag = false;
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
			bufr = new BufferedReader(isr);
//			bufr = new BufferedReader(new FileReader(file));
			String line = bufr.readLine();
			
			if(line != null && line.contains(containsStr)) {
				flag = true;
			}
	        
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(bufr != null) {
					bufr.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return flag;
    }
	
    /**
     * 在指定文本之前插入一段文本
     * 
     * @param targetStr
     * @param insertStr
     * @param replaceStr
     * @return void:
     * @version 1.0 
     * @date 2015-9-2
     * @Author zhou.wenkai
     */
    public void insertFrontText(File file, String targetStr, String insertStr) {
    	BufferedReader	bufr = null;
    	BufferedWriter 	bufw = null;
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
			bufr = new BufferedReader(isr);
//			bufr = new BufferedReader(new FileReader(file));
			StringBuffer buf = new StringBuffer();
			String line = null;
			
			while ((line = bufr.readLine()) != null) {
				// 保存要插入文本
				if(line.contains(targetStr)) {
					buf.append(insertStr);
				}
				// 保存原文本
				buf = buf.append(line);
				// 添加换行符号
				buf = buf.append(System.getProperty("line.separator"));
			}
			OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
			bufw = new BufferedWriter(osw);
//			bufw = new BufferedWriter(new FileWriter(file));
			bufw.write(buf.toString());
            bufw.close();
	        
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(bufr != null) {
					bufr.close();
				}
				if(bufw != null) {
					bufw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    }
    
    /**
     * 在指定文本之后插入一段文本
     * 
     * @param targetStr
     * @param insertStr
     * @param replaceStr
     * @return void:
     * @version 1.0 
     * @date 2015-9-2
     * @Author zhou.wenkai
     */
    public void insertBehindText(File file, String targetStr, String insertStr) {
    	BufferedReader	bufr = null;
    	BufferedWriter 	bufw = null;
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
			bufr = new BufferedReader(isr);
			
//			bufr = new BufferedReader(new FileReader(file));
			StringBuffer buf = new StringBuffer();
			String line = null;
			
			while ((line = bufr.readLine()) != null) {
				// 保存原文本
				buf = buf.append(line);
				// 保存要插入文本
				if(line.contains(targetStr)) {
					buf.append(insertStr);
				}
				// 添加换行符号
				buf = buf.append(System.getProperty("line.separator"));
			}
			OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
			bufw = new BufferedWriter(osw);
//			bufw = new BufferedWriter(new FileWriter(file));
			bufw.write(buf.toString());
            bufw.close();
	        
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(bufr != null) {
					bufr.close();
				}
				if(bufw != null) {
					bufw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    }
    
    /**
     * 在指定文本之后插入一段文本
     * 
     * @param targetStr1 第一行
     * @param targetStr2 第二行
     * @param insertStr 要插入内容
     * @param replaceStr
     * @return void:
     * @version 1.0 
     * @date 2015-9-6
     * @Author zhou.wenkai
     */
    public void insertSecondBehindText(File file, String targetStr1, String targetStr2, String insertStr) {
		LineNumberReader lnr  = null;
    	BufferedWriter 	 bufw = null;
    	boolean hasFirstLineNum	= false;
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
			lnr = new LineNumberReader(isr);
//			lnr = new LineNumberReader(new FileReader(file));
			StringBuffer buf = new StringBuffer();
			String line = null;
			int lineNumber = 0;
			while ((line = lnr.readLine()) != null) {
				
				// 保存原文本
				buf = buf.append(line);
				// 添加换行符号
				buf = buf.append(System.getProperty("line.separator"));
				
				// 保存要插入文本
				if(line.contains(targetStr1) && !hasFirstLineNum) {
					lineNumber = lnr.getLineNumber();
					hasFirstLineNum = true;
				}
				if(line.contains(targetStr2) && hasFirstLineNum) {
					if((lnr.getLineNumber() - lineNumber) == 1) {
						buf.append(insertStr);
						buf = buf.append(System.getProperty("line.separator"));
					}
				}
			}
			OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
			bufw = new BufferedWriter(osw);
//			bufw = new BufferedWriter(new FileWriter(file));
			bufw.write(buf.toString());
            bufw.close();
	        
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(lnr != null) {
					lnr.close();
				}
				if(bufw != null) {
					bufw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    }
    
    /** 
     * 在指定两行之间插入
     * 
     * @param file
     * @param frontStr 上一行内容
     * @param behindStr 下一行内容
     * @param insertStr  要插入的内容
     * @return void:
     * @version 1.0 
     * @date 2015-9-3
     * @Author zhou.wenkai
     */
    public void insertMiddleText(File file, String frontStr, String behindStr, String insertStr) {
    	insertMiddleText(file, frontStr, behindStr, insertStr, 0);
    }
    
    /** 
     * 在指定间隔空白行数目的两行之间插入内容
     * 
     * @param file
     * @param frontStr  上一行内容
     * @param behindStr 下一行内容
     * @param insertStr 要插入的内容
     * @param blankLine
     * @return void:
     * @version 1.0 
     * @date 2015-9-3
     * @Author zwenkai
     */
	public void insertMiddleText(File file, String frontStr, String behindStr,
			String insertStr, int blankLine) {
		LineNumberReader lnr  = null;
    	BufferedWriter 	 bufw = null;
    	boolean hasFrontLineNum	= false;
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
			lnr = new LineNumberReader(isr);
//			lnr = new LineNumberReader(new FileReader(file));
			StringBuffer buf = new StringBuffer();
			String line = null;
			int lineNumber = 0;
			while ((line = lnr.readLine()) != null) {
				
				// 保存要插入文本
				if(line.contains(frontStr) && !hasFrontLineNum) {
					lineNumber = lnr.getLineNumber();
					hasFrontLineNum = true;
				}
				if(line.contains(behindStr) && hasFrontLineNum) {
					if((lnr.getLineNumber() - lineNumber) == blankLine + 1) {
						buf.append(insertStr);
						buf = buf.append(System.getProperty("line.separator"));
					}
				}
				
				// 保存原文本
				buf = buf.append(line);
				// 添加换行符号
				buf = buf.append(System.getProperty("line.separator"));
			}
			
			OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
			bufw = new BufferedWriter(osw);
//			bufw = new BufferedWriter(new FileWriter(file));
			bufw.write(buf.toString());
            bufw.close();
	        
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(lnr != null) {
					lnr.close();
				}
				if(bufw != null) {
					bufw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
    /**
     * 将指定文本替换
     * 
     * @param file
     * @param targetStr
     * @param replaceStr
     * @return void:
     * @version 1.0 
     * @date 2015-9-2
     * @Author zhou.wenkai
     */
    public void replaseLineText(File file, String targetStr, String replaseStr) {
    	BufferedReader bufr = null;
    	BufferedWriter bufw = null;
		try {
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
			bufr = new BufferedReader(isr);
//			bufr = new BufferedReader(new FileReader(file));
			StringBuffer buf = new StringBuffer();
			String line = null;
			// 保存该行前面的内容
			while ((line = bufr.readLine()) != null) {
				
				if(line.contains(targetStr)) {
					buf.append(replaseStr);	// 插入要替换
				} else{
					buf = buf.append(line);
				}
				buf = buf.append(System.getProperty("line.separator"));
			}
            
			OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
			bufw = new BufferedWriter(osw);
//			bufw = new BufferedWriter(new FileWriter(file));
			bufw.write(buf.toString());
            bufw.close();
	        
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(bufr != null) {
					bufr.close();
				}
				if(bufw != null) {
					bufw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    }

}
